﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ScFramework.Core;
using System;

public interface ICombineSubUI
{
    void InitGameobject(List<GameObject> obj);
}
public class CombineUISubmodule : UISubmodule<CombineUI>
{
    protected Vector3 CenterPos;

    protected int CombineBenchSiz = 7;

    List<MeshEntityData> _meshData = new List<MeshEntityData>();
    public override void OnInit(object args)
    {
        base.OnInit(args);
        Debug.Assert(args is Vector3);
        CenterPos = (Vector3)args;
        var list = Gets<ICombineSubUI>();
        foreach (var x in list)
        {
            x.InitGameobject(openedUI.Subs);
        }
        _main.Get<UserManager>().RegisterUserEvent(WhenFromCombineToGame, UserState.OnCombine, UserState.OnGame, -1);
        GetSystemModule<MessageManager>().Get<CombineUIAction>().RegisterHandler(CombineUIAction.StartCombine, (object s,EventArgs e) =>
        {
            if(e is ObjectArgs<Vector3> v)
            {
                var vtr = GameObject.FindObjectOfType<Camera>().gameObject.transform.position;
                GameObject.FindObjectOfType<Camera>().gameObject.transform.position = new Vector3(v.obj.x, v.obj.y, vtr.z);
                CenterPos = v.obj;
                Debug.Assert(this.Enable == false);
                this.Enable = true;
            }
            else
            {
                throw new Exception();
            }
            return null;
        });
    }
    void WhenFromCombineToGame(int id)
    {
        if (_main.Get<UserManager>().UserID == id)
        {
            foreach(var x in _meshData)
            {
                if (x.Body.PresentPos == 1)
                {
                    foreach(var c in x.Colliders)
                    {
                        c.LocalRotation = x.Body.MeshRotate * 90;
                        c.LocalIsFliped = x.Body.IsFliped;
                        c.LocalPosition = CenterPos + (Vector3)x.Body.MeshPos * Main.ObejectBlockSize - (Vector3)Vector2.one * (CombineBenchSiz / 2) * Main.ObejectBlockSize;

                    }
                }
            }
        }
        Enable = false;
    }
    public override void OnEnable()
    {
        base.OnEnable();
        openedUI.gameObject.SetActive(true);

    }
    public override void OnDisable()
    {
        base.OnDisable();
        openedUI.gameObject.SetActive(false);
    }
    [RegisterEvent(LogicEntityDataEvent.Receive_Data)]
    void ReceieveData(List<MeshEntityData> datas)
    {
        _meshData = datas.FindAll(s => s.Body.UserID == _main.Get<UserManager>().UserID);
        Dispatch(CombineUIEvent.Receive_Data, new List<MeshEntityData>(_meshData));
    }
    [RegisterEvent(CombineUIEvent.Start)]
    void OnStartGame(object args=null)
    {
        Debug.Assert(GetSystemModule<UserManager>().GetUserState(GetSystemModule<UserManager>().UserID) == UserState.OnCombine);
        var benchItem = _meshData.FindAll(s => s.Body.PresentPos == (int)ObjectPresentPos.Bench);
        var list = new List<SColliderVEx>();
        foreach(var x in benchItem)
        {
            list.AddRange(x.Colliders);
        }
       if( GameMode.Instance.CheckIfCanToGame(list)){
            GetSystemModule<UserManager>().SetUserState(GetSystemModule<UserManager>().UserID, UserState.OnGame);
        }
        else
        {
            GetSystemModule<UIManager>().Open("DialogWindowUI", 5, args: new DialogWindowUI.DialogWindowUIArg
            ("注意", "核心没有设置正确", null, "确定"));
        }
    
    }
}
public enum CombineUIAction
{
    StartCombine,
    ExitCombine
}


public enum FunctionalUIEvent
{
    ShowKeycode,
    ShowMassCenter,
    LayerChanged,
}


public enum CombineUIEvent
{


    Start,

    Receive_Data,
    /// <summary>
    /// 当有物体被选中时,发送此消息,参数为所选中物体的id<int>,若没有选中物体则参数的值为-1
    /// </summary>
    ShowObjectValues,
    /// <summary>
    /// 当有物体被拖动时,发送此消息,参数为所选中的物体的id</int>,若没有拖动物体则参数的值为-1
    /// </summary>
    DragObject,

    SpinEntity,

    FlipEntity,

    /// <summary>
    /// 当键位改变时改变时，发送此消息
    /// </summary>
    KeyChanged,
    /// <summary>
    /// 按键改变前
    /// </summary>
    KeyChangedBefore,
    /// <summary>
    /// 当背包类型改变时发送此消息
    /// </summary>
    BagTypeChanged,
    /// <summary>
    /// 拼装台到背包
    /// </summary>
    BenchToBag,
    /// <summary>
    /// 背包到拼装台
    /// </summary>
    BagToBench,
}
public enum ItemType
{
    All,
    Joint,
    Weapon,
    Assist,
}
public class CombinedFuncArgs : System.EventArgs
{
    public List<FuncDataPair> Datas;
}

public class FuncDataPair
{
    public SFuncBaseVEx Func;
    public Vector3Int EntityPos;
    public SComBodyVEx Body;
}
